home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / sound / adpcm.c < prev    next >
C/C++ Source or Header  |  2000-04-23  |  23KB  |  817 lines

  1. /**********************************************************************************************
  2.  *
  3.  *   streaming ADPCM driver
  4.  *   by Aaron Giles
  5.  *
  6.  *   Library to transcode from an ADPCM source to raw PCM.
  7.  *   Written by Buffoni Mirko in 08/06/97
  8.  *   References: various sources and documents.
  9.  *
  10.  *     HJB 08/31/98
  11.  *     modified to use an automatically selected oversampling factor
  12.  *     for the current Machine->sample_rate
  13.  *
  14.  *   Mish 21/7/99
  15.  *   Updated to allow multiple OKI chips with different sample rates
  16.  *
  17.  **********************************************************************************************/
  18.  
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <math.h>
  23.  
  24. #include "driver.h"
  25. #include "adpcm.h"
  26.  
  27.  
  28. #define MAX_SAMPLE_CHUNK    10000
  29.  
  30. #define FRAC_BITS            14
  31. #define FRAC_ONE            (1 << FRAC_BITS)
  32. #define FRAC_MASK            (FRAC_ONE - 1)
  33.  
  34.  
  35. /* struct describing a single playing ADPCM voice */
  36. struct ADPCMVoice
  37. {
  38.     int stream;                /* which stream are we playing on? */
  39.     UINT8 playing;            /* 1 if we are actively playing */
  40.  
  41.     UINT8 *region_base;        /* pointer to the base of the region */
  42.     UINT8 *base;            /* pointer to the base memory location */
  43.     UINT32 sample;            /* current sample number */
  44.     UINT32 count;            /* total samples to play */
  45.  
  46.     UINT32 signal;            /* current ADPCM signal */
  47.     UINT32 step;            /* current ADPCM step */
  48.     UINT32 volume;            /* output volume */
  49.  
  50.     INT16 last_sample;        /* last sample output */
  51.     INT16 curr_sample;        /* current sample target */
  52.     UINT32 source_step;        /* step value for frequency conversion */
  53.     UINT32 source_pos;        /* current fractional position */
  54. };
  55.  
  56. /* array of ADPCM voices */
  57. static UINT8 num_voices;
  58. static struct ADPCMVoice adpcm[MAX_ADPCM];
  59.  
  60. /* global pointer to the current array of samples */
  61. static struct ADPCMsample *sample_list;
  62.  
  63. /* step size index shift table */
  64. static int index_shift[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
  65.  
  66. /* lookup table for the precomputed difference */
  67. static int diff_lookup[49*16];
  68.  
  69. /* volume lookup table */
  70. static UINT32 volume_table[16];
  71.  
  72.  
  73.  
  74. /**********************************************************************************************
  75.  
  76.      compute_tables -- compute the difference tables
  77.  
  78. ***********************************************************************************************/
  79.  
  80. static void compute_tables(void)
  81. {
  82.     /* nibble to bit map */
  83.     static int nbl2bit[16][4] =
  84.     {
  85.         { 1, 0, 0, 0}, { 1, 0, 0, 1}, { 1, 0, 1, 0}, { 1, 0, 1, 1},
  86.         { 1, 1, 0, 0}, { 1, 1, 0, 1}, { 1, 1, 1, 0}, { 1, 1, 1, 1},
  87.         {-1, 0, 0, 0}, {-1, 0, 0, 1}, {-1, 0, 1, 0}, {-1, 0, 1, 1},
  88.         {-1, 1, 0, 0}, {-1, 1, 0, 1}, {-1, 1, 1, 0}, {-1, 1, 1, 1}
  89.     };
  90.  
  91.     int step, nib;
  92.  
  93.     /* loop over all possible steps */
  94.     for (step = 0; step <= 48; step++)
  95.     {
  96.         /* compute the step value */
  97.         int stepval = floor(16.0 * pow(11.0 / 10.0, (double)step));
  98.  
  99.         /* loop over all nibbles and compute the difference */
  100.         for (nib = 0; nib < 16; nib++)
  101.         {
  102.             diff_lookup[step*16 + nib] = nbl2bit[nib][0] *
  103.                 (stepval   * nbl2bit[nib][1] +
  104.                  stepval/2 * nbl2bit[nib][2] +
  105.                  stepval/4 * nbl2bit[nib][3] +
  106.                  stepval/8);
  107.         }
  108.     }
  109.  
  110.     /* generate the volume table (currently just a guess) */
  111.     for (step = 0; step < 16; step++)
  112.     {
  113.         double out = 256.0;
  114.         int vol = step;
  115.  
  116.         /* assume 2dB per step (most likely wrong!) */
  117.         while (vol-- > 0)
  118.             out /= 1.258925412;    /* = 10 ^ (2/20) = 2dB */
  119.         volume_table[step] = (UINT32)out;
  120.     }
  121. }
  122.  
  123.  
  124.  
  125. /**********************************************************************************************
  126.  
  127.      generate_adpcm -- general ADPCM decoding routine
  128.  
  129. ***********************************************************************************************/
  130.  
  131. static void generate_adpcm(struct ADPCMVoice *voice, INT16 *buffer, int samples)
  132. {
  133.     /* if this voice is active */
  134.     if (voice->playing)
  135.     {
  136.         UINT8 *base = voice->base;
  137.         int sample = voice->sample;
  138.         int signal = voice->signal;
  139.         int count = voice->count;
  140.         int step = voice->step;
  141.         int val;
  142.  
  143.         /* loop while we still have samples to generate */
  144.         while (samples)
  145.         {
  146.             /* compute the new amplitude and update the current step */
  147.             val = base[sample / 2] >> (((sample & 1) << 2) ^ 4);
  148.             signal += diff_lookup[step * 16 + (val & 15)];
  149.  
  150.             /* clamp to the maximum */
  151.             if (signal > 2047)
  152.                 signal = 2047;
  153.             else if (signal < -2048)
  154.                 signal = -2048;
  155.  
  156.             /* adjust the step size and clamp */
  157.             step += index_shift[val & 7];
  158.             if (step > 48)
  159.                 step = 48;
  160.             else if (step < 0)
  161.                 step = 0;
  162.  
  163.             /* output to the buffer, scaling by the volume */
  164.             *buffer++ = signal * voice->volume / 16;
  165.             samples--;
  166.  
  167.             /* next! */
  168.             if (++sample > count)
  169.             {
  170.                 voice->playing = 0;
  171.                 break;
  172.             }
  173.         }
  174.  
  175.         /* update the parameters */
  176.         voice->sample = sample;
  177.         voice->signal = signal;
  178.         voice->step = step;
  179.     }
  180.  
  181.     /* fill the rest with silence */
  182.     while (samples--)
  183.         *buffer++ = 0;
  184. }
  185.  
  186.  
  187.  
  188. /**********************************************************************************************
  189.  
  190.      adpcm_update -- update the sound chip so that it is in sync with CPU execution
  191.  
  192. ***********************************************************************************************/
  193.  
  194. static void adpcm_update(int num, INT16 *buffer, int length)
  195. {
  196.     struct ADPCMVoice *voice = &adpcm[num];
  197.     INT16 sample_data[MAX_SAMPLE_CHUNK], *curr_data = sample_data;
  198.     INT16 prev = voice->last_sample, curr = voice->curr_sample;
  199.     UINT32 final_pos;
  200.     UINT32 new_samples;
  201.  
  202.     /* finish off the current sample */
  203.     if (voice->source_pos > 0)
  204.     {
  205.         /* interpolate */
  206.         while (length > 0 && voice->source_pos < FRAC_ONE)
  207.         {
  208.             *buffer++ = (((INT32)prev * (FRAC_ONE - voice->source_pos)) + ((INT32)curr * voice->source_pos)) >> FRAC_BITS;
  209.             voice->source_pos += voice->source_step;
  210.             length--;
  211.         }
  212.  
  213.         /* if we're over, continue; otherwise, we're done */
  214.         if (voice->source_pos >= FRAC_ONE)
  215.             voice->source_pos -= FRAC_ONE;
  216.         else
  217.             return;
  218.     }
  219.  
  220.     /* compute how many new samples we need */
  221.     final_pos = voice->source_pos + length * voice->source_step;
  222.     new_samples = (final_pos + FRAC_ONE - 1) >> FRAC_BITS;
  223.     if (new_samples > MAX_SAMPLE_CHUNK)
  224.         new_samples = MAX_SAMPLE_CHUNK;
  225.  
  226.     /* generate them into our buffer */
  227.     generate_adpcm(voice, sample_data, new_samples);
  228.     prev = curr;
  229.     curr = *curr_data++;
  230.  
  231.     /* then sample-rate convert with linear interpolation */
  232.     while (length > 0)
  233.     {
  234.         /* interpolate */
  235.         while (length > 0 && voice->source_pos < FRAC_ONE)
  236.         {
  237.             *buffer++ = (((INT32)prev * (FRAC_ONE - voice->source_pos)) + ((INT32)curr * voice->source_pos)) >> FRAC_BITS;
  238.             voice->source_pos += voice->source_step;
  239.             length--;
  240.         }
  241.  
  242.         /* if we're over, grab the next samples */
  243.         if (voice->source_pos >= FRAC_ONE)
  244.         {
  245.             voice->source_pos -= FRAC_ONE;
  246.             prev = curr;
  247.             curr = *curr_data++;
  248.         }
  249.     }
  250.  
  251.     /* remember the last samples */
  252.     voice->last_sample = prev;
  253.     voice->curr_sample = curr;
  254. }
  255.  
  256.  
  257.  
  258. /**********************************************************************************************
  259.  
  260.      ADPCM_sh_start -- start emulation of several ADPCM output streams
  261.  
  262. ***********************************************************************************************/
  263.  
  264. int ADPCM_sh_start(const struct MachineSound *msound)
  265. {
  266.     const struct ADPCMinterface *intf = msound->sound_interface;
  267.     char stream_name[40];
  268.     int i;
  269.  
  270.     /* reset the ADPCM system */
  271.     num_voices = intf->num;
  272.     compute_tables();
  273.     sample_list = 0;
  274.  
  275.     /* generate the sample table, if one is needed */
  276.     if (intf->init)
  277.     {
  278.         /* allocate memory for it */
  279.         sample_list = malloc(257 * sizeof(struct ADPCMsample));
  280.         if (!sample_list)
  281.             return 1;
  282.         memset(sample_list, 0, 257 * sizeof(struct ADPCMsample));
  283.  
  284.         /* callback to initialize */
  285.         (*intf->init)(intf, sample_list, 256);
  286.     }
  287.  
  288.     /* initialize the voices */
  289.     memset(adpcm, 0, sizeof(adpcm));
  290.     for (i = 0; i < num_voices; i++)
  291.     {
  292.         /* generate the name and create the stream */
  293.         sprintf(stream_name, "%s #%d", sound_name(msound), i);
  294.         adpcm[i].stream = stream_init(stream_name, intf->mixing_level[i], Machine->sample_rate, i, adpcm_update);
  295.         if (adpcm[i].stream == -1)
  296.             return 1;
  297.  
  298.         /* initialize the rest of the structure */
  299.         adpcm[i].region_base = memory_region(intf->region);
  300.         adpcm[i].volume = 255;
  301.         adpcm[i].signal = -2;
  302.         if (Machine->sample_rate)
  303.             adpcm[i].source_step = (UINT32)((double)intf->frequency * (double)FRAC_ONE / (double)Machine->sample_rate);
  304.     }
  305.  
  306.     /* success */
  307.     return 0;
  308. }
  309.  
  310.  
  311.  
  312. /**********************************************************************************************
  313.  
  314.      ADPCM_sh_stop -- stop emulation of several ADPCM output streams
  315.  
  316. ***********************************************************************************************/
  317.  
  318. void ADPCM_sh_stop(void)
  319. {
  320.     /* free the temporary table if we created it */
  321.     if (sample_list)
  322.     {
  323.         free(sample_list);
  324.         sample_list = 0;
  325.     }
  326. }
  327.  
  328.  
  329.  
  330. /**********************************************************************************************
  331.  
  332.      ADPCM_sh_update -- update ADPCM streams
  333.  
  334. ***********************************************************************************************/
  335.  
  336. void ADPCM_sh_update(void)
  337. {
  338. }
  339.  
  340.  
  341.  
  342. /**********************************************************************************************
  343.  
  344.      ADPCM_trigger -- handle a write to the ADPCM data stream
  345.  
  346. ***********************************************************************************************/
  347.  
  348. void ADPCM_trigger(int num, int which)
  349. {
  350.     struct ADPCMVoice *voice = &adpcm[num];
  351.     struct ADPCMsample *sample;
  352.  
  353.     /* bail if we're not playing anything */
  354.     if (Machine->sample_rate == 0)
  355.         return;
  356.  
  357.     /* range check the numbers */
  358.     if (num >= num_voices)
  359.     {
  360.         logerror("error: ADPCM_trigger() called with channel = %d, but only %d channels allocated\n", num, num_voices);
  361.         return;
  362.     }
  363.  
  364.     /* find a match */
  365.     for (sample = sample_list; sample->length > 0; sample++)
  366.         if (sample->num == which)
  367.         {
  368.             /* update the ADPCM voice */
  369.             stream_update(voice->stream, 0);
  370.  
  371.             /* set up the voice to play this sample */
  372.             voice->playing = 1;
  373.             voice->base = &voice->region_base[sample->offset];
  374.             voice->sample = 0;
  375.             voice->count = sample->length;
  376.  
  377.             /* also reset the ADPCM parameters */
  378.             voice->signal = -2;
  379.             voice->step = 0;
  380.             return;
  381.         }
  382.  
  383.     logerror("warning: ADPCM_trigger() called with unknown trigger = %08x\n",which);
  384. }
  385.  
  386.  
  387.  
  388. /**********************************************************************************************
  389.  
  390.      ADPCM_play -- play data from a specific offset for a specific length
  391.  
  392. ***********************************************************************************************/
  393.  
  394. void ADPCM_play(int num, int offset, int length)
  395. {
  396.     struct ADPCMVoice *voice = &adpcm[num];
  397.  
  398.     /* bail if we're not playing anything */
  399.     if (Machine->sample_rate == 0)
  400.         return;
  401.  
  402.     /* range check the numbers */
  403.     if (num >= num_voices)
  404.     {
  405.         logerror("error: ADPCM_trigger() called with channel = %d, but only %d channels allocated\n", num, num_voices);
  406.         return;
  407.     }
  408.  
  409.     /* update the ADPCM voice */
  410.     stream_update(voice->stream, 0);
  411.  
  412.     /* set up the voice to play this sample */
  413.     voice->playing = 1;
  414.     voice->base = &voice->region_base[offset];
  415.     voice->sample = 0;
  416.     voice->count = length;
  417.  
  418.     /* also reset the ADPCM parameters */
  419.     voice->signal = -2;
  420.     voice->step = 0;
  421. }
  422.  
  423.  
  424.  
  425. /**********************************************************************************************
  426.  
  427.      ADPCM_play -- stop playback on an ADPCM data channel
  428.  
  429. ***********************************************************************************************/
  430.  
  431. void ADPCM_stop(int num)
  432. {
  433.     struct ADPCMVoice *voice = &adpcm[num];
  434.  
  435.     /* bail if we're not playing anything */
  436.     if (Machine->sample_rate == 0)
  437.         return;
  438.  
  439.     /* range check the numbers */
  440.     if (num >= num_voices)
  441.     {
  442.         logerror("error: ADPCM_stop() called with channel = %d, but only %d channels allocated\n", num, num_voices);
  443.         return;
  444.     }
  445.  
  446.     /* update the ADPCM voice */
  447.     stream_update(voice->stream, 0);
  448.  
  449.     /* stop playback */
  450.     voice->playing = 0;
  451. }
  452.  
  453.  
  454.  
  455. /**********************************************************************************************
  456.  
  457.      ADPCM_setvol -- change volume on an ADPCM data channel
  458.  
  459. ***********************************************************************************************/
  460.  
  461. void ADPCM_setvol(int num, int vol)
  462. {
  463.     struct ADPCMVoice *voice = &adpcm[num];
  464.  
  465.     /* bail if we're not playing anything */
  466.     if (Machine->sample_rate == 0)
  467.         return;
  468.  
  469.     /* range check the numbers */
  470.     if (num >= num_voices)
  471.     {
  472.         logerror("error: ADPCM_setvol() called with channel = %d, but only %d channels allocated\n", num, num_voices);
  473.         return;
  474.     }
  475.  
  476.     /* update the ADPCM voice */
  477.     stream_update(voice->stream, 0);
  478.     voice->volume = vol;
  479. }
  480.  
  481.  
  482.  
  483. /**********************************************************************************************
  484.  
  485.      ADPCM_playing -- returns true if an ADPCM data channel is still playing
  486.  
  487. ***********************************************************************************************/
  488.  
  489. int ADPCM_playing(int num)
  490. {
  491.     struct ADPCMVoice *voice = &adpcm[num];
  492.  
  493.     /* bail if we're not playing anything */
  494.     if (Machine->sample_rate == 0)
  495.         return 0;
  496.  
  497.     /* range check the numbers */
  498.     if (num >= num_voices)
  499.     {
  500.         logerror("error: ADPCM_playing() called with channel = %d, but only %d channels allocated\n", num, num_voices);
  501.         return 0;
  502.     }
  503.  
  504.     /* update the ADPCM voice */
  505.     stream_update(voice->stream, 0);
  506.     return voice->playing;
  507. }
  508.  
  509.  
  510.  
  511. /**********************************************************************************************
  512.  *
  513.  *    OKIM 6295 ADPCM chip:
  514.  *
  515.  *    Command bytes are sent:
  516.  *
  517.  *        1xxx xxxx = start of 2-byte command sequence, xxxxxxx is the sample number to trigger
  518.  *        abcd vvvv = second half of command; one of the abcd bits is set to indicate which voice
  519.  *                    the v bits seem to be volumed
  520.  *
  521.  *        0abc d000 = stop playing; one or more of the abcd bits is set to indicate which voice(s)
  522.  *
  523.  *    Status is read:
  524.  *
  525.  *        ???? abcd = one bit per voice, set to 0 if nothing is playing, or 1 if it is active
  526.  *
  527. ***********************************************************************************************/
  528.  
  529. static int okim6295_command[MAX_OKIM6295];
  530. static int okim6295_base[MAX_OKIM6295][MAX_OKIM6295_VOICES];
  531.  
  532.  
  533. /**********************************************************************************************
  534.  
  535.      OKIM6295_sh_start -- start emulation of an OKIM6295-compatible chip
  536.  
  537. ***********************************************************************************************/
  538.  
  539. int OKIM6295_sh_start(const struct MachineSound *msound)
  540. {
  541.     const struct OKIM6295interface *intf = msound->sound_interface;
  542.     char stream_name[40];
  543.     int i;
  544.  
  545.     /* reset the ADPCM system */
  546.     num_voices = intf->num * MAX_OKIM6295_VOICES;
  547.     compute_tables();
  548.     sample_list = 0;
  549.  
  550.     /* initialize the voices */
  551.     memset(adpcm, 0, sizeof(adpcm));
  552.     for (i = 0; i < num_voices; i++)
  553.     {
  554.         int chip = i / MAX_OKIM6295_VOICES;
  555.         int voice = i % MAX_OKIM6295_VOICES;
  556.  
  557.         /* reset the OKI-specific parameters */
  558.         okim6295_command[chip] = -1;
  559.         okim6295_base[chip][voice] = 0;
  560.  
  561.         /* generate the name and create the stream */
  562.         sprintf(stream_name, "%s #%d (voice %d)", sound_name(msound), chip, voice);
  563.         adpcm[i].stream = stream_init(stream_name, intf->mixing_level[chip], Machine->sample_rate, i, adpcm_update);
  564.         if (adpcm[i].stream == -1)
  565.             return 1;
  566.  
  567.         /* initialize the rest of the structure */
  568.         adpcm[i].region_base = memory_region(intf->region[chip]);
  569.         adpcm[i].volume = 255;
  570.         adpcm[i].signal = -2;
  571.         if (Machine->sample_rate)
  572.             adpcm[i].source_step = (UINT32)((double)intf->frequency[chip] * (double)FRAC_ONE / (double)Machine->sample_rate);
  573.     }
  574.  
  575.     /* success */
  576.     return 0;
  577. }
  578.  
  579.  
  580.  
  581. /**********************************************************************************************
  582.  
  583.      OKIM6295_sh_stop -- stop emulation of an OKIM6295-compatible chip
  584.  
  585. ***********************************************************************************************/
  586.  
  587. void OKIM6295_sh_stop(void)
  588. {
  589. }
  590.  
  591.  
  592.  
  593. /**********************************************************************************************
  594.  
  595.      OKIM6295_sh_update -- update emulation of an OKIM6295-compatible chip
  596.  
  597. ***********************************************************************************************/
  598.  
  599. void OKIM6295_sh_update(void)
  600. {
  601. }
  602.  
  603.  
  604.  
  605. /**********************************************************************************************
  606.  
  607.      OKIM6295_set_bank_base -- set the base of the bank for a given voice on a given chip
  608.  
  609. ***********************************************************************************************/
  610.  
  611. void OKIM6295_set_bank_base(int which, int channel, int base)
  612. {
  613.     struct ADPCMVoice *voice = &adpcm[which * MAX_OKIM6295_VOICES + channel];
  614.  
  615.     /* handle the all voice case */
  616.     if (channel == ALL_VOICES)
  617.     {
  618.         int i;
  619.  
  620.         for (i = 0; i < MAX_OKIM6295_VOICES; i++)
  621.             OKIM6295_set_bank_base(which, i, base);
  622.         return;
  623.     }
  624.  
  625.     /* update the stream and set the new base */
  626.     stream_update(voice->stream, 0);
  627.     okim6295_base[which][channel] = base;
  628. }
  629.  
  630.  
  631.  
  632. /**********************************************************************************************
  633.  
  634.      OKIM6295_set_frequency -- dynamically adjusts the frequency of a given ADPCM voice
  635.  
  636. ***********************************************************************************************/
  637.  
  638. void OKIM6295_set_frequency(int which, int channel, int frequency)
  639. {
  640.     struct ADPCMVoice *voice = &adpcm[which * MAX_OKIM6295_VOICES + channel];
  641.  
  642.     /* handle the all voice case */
  643.     if (channel == ALL_VOICES)
  644.     {
  645.         int i;
  646.  
  647.         for (i = 0; i < MAX_OKIM6295_VOICES; i++)
  648.             OKIM6295_set_frequency(which, i, frequency);
  649.         return;
  650.     }
  651.  
  652.     /* update the stream and set the new base */
  653.     stream_update(voice->stream, 0);
  654.     if (Machine->sample_rate)
  655.         voice->source_step = (UINT32)((double)frequency * (double)FRAC_ONE / (double)Machine->sample_rate);
  656. }
  657.  
  658.  
  659. /**********************************************************************************************
  660.  
  661.      OKIM6295_status_r -- read the status port of an OKIM6295-compatible chip
  662.  
  663. ***********************************************************************************************/
  664.  
  665. static int OKIM6295_status_r(int num)
  666. {
  667.     int i, result;
  668.  
  669.     /* range check the numbers */
  670.     if (num >= num_voices / MAX_OKIM6295_VOICES)
  671.     {
  672.         logerror("error: OKIM6295_status_r() called with chip = %d, but only %d chips allocated\n",num, num_voices / MAX_OKIM6295_VOICES);
  673.         return 0x0f;
  674.     }
  675.  
  676.     /* set the bit to 1 if something is playing on a given channel */
  677.     result = 0;
  678.     for (i = 0; i < MAX_OKIM6295_VOICES; i++)
  679.     {
  680.         struct ADPCMVoice *voice = &adpcm[num * MAX_OKIM6295_VOICES + i];
  681.  
  682.         /* update the stream */
  683.         stream_update(voice->stream, 0);
  684.  
  685.         /* set the bit if it's playing */
  686.         if (voice->playing)
  687.             result |= 1 << i;
  688.     }
  689.  
  690.     return result;
  691. }
  692.  
  693.  
  694.  
  695. /**********************************************************************************************
  696.  
  697.      OKIM6295_data_w -- write to the data port of an OKIM6295-compatible chip
  698.  
  699. ***********************************************************************************************/
  700.  
  701. static void OKIM6295_data_w(int num, int data)
  702. {
  703.     /* range check the numbers */
  704.     if (num >= num_voices / MAX_OKIM6295_VOICES)
  705.     {
  706.         logerror("error: OKIM6295_data_w() called with chip = %d, but only %d chips allocated\n", num, num_voices / MAX_OKIM6295_VOICES);
  707.         return;
  708.     }
  709.  
  710.     /* if a command is pending, process the second half */
  711.     if (okim6295_command[num] != -1)
  712.     {
  713.         int temp = data >> 4, i, start, stop;
  714.         unsigned char *base;
  715.  
  716.         /* determine which voice(s) (voice is set by a 1 bit in the upper 4 bits of the second byte) */
  717.         for (i = 0; i < MAX_OKIM6295_VOICES; i++, temp >>= 1)
  718.             if (temp & 1)
  719.             {
  720.                 struct ADPCMVoice *voice = &adpcm[num * MAX_OKIM6295_VOICES + i];
  721.  
  722.                 /* update the stream */
  723.                 stream_update(voice->stream, 0);
  724.  
  725.                 /* determine the start/stop positions */
  726.                 base = &voice->region_base[okim6295_base[num][i] + okim6295_command[num] * 8];
  727.                 start = (base[0] << 16) + (base[1] << 8) + base[2];
  728.                 stop = (base[3] << 16) + (base[4] << 8) + base[5];
  729.  
  730.                 /* set up the voice to play this sample */
  731.                 if (start < 0x40000 && stop < 0x40000)
  732.                 {
  733.                     voice->playing = 1;
  734.                     voice->base = &voice->region_base[okim6295_base[num][i] + start];
  735.                     voice->sample = 0;
  736.                     voice->count = 2 * (stop - start + 1);
  737.  
  738.                     /* also reset the ADPCM parameters */
  739.                     voice->signal = -2;
  740.                     voice->step = 0;
  741.                     voice->volume = volume_table[data & 0x0f];
  742.                 }
  743.  
  744.                 /* invalid samples go here */
  745.                 else
  746.                 {
  747.                     logerror("OKIM6295: requested to play invalid sample %02x\n",okim6295_command[num]);
  748.                     voice->playing = 0;
  749.                 }
  750.             }
  751.  
  752.         /* reset the command */
  753.         okim6295_command[num] = -1;
  754.     }
  755.  
  756.     /* if this is the start of a command, remember the sample number for next time */
  757.     else if (data & 0x80)
  758.     {
  759.         okim6295_command[num] = data & 0x7f;
  760.     }
  761.  
  762.     /* otherwise, see if this is a silence command */
  763.     else
  764.     {
  765.         int temp = data >> 3, i;
  766.  
  767.         /* determine which voice(s) (voice is set by a 1 bit in bits 3-6 of the command */
  768.         for (i = 0; i < 4; i++, temp >>= 1)
  769.             if (temp & 1)
  770.             {
  771.                 struct ADPCMVoice *voice = &adpcm[num * MAX_OKIM6295_VOICES + i];
  772.  
  773.                 /* update the stream, then turn it off */
  774.                 stream_update(voice->stream, 0);
  775.                 voice->playing = 0;
  776.             }
  777.     }
  778. }
  779.  
  780.  
  781.  
  782. /**********************************************************************************************
  783.  
  784.      OKIM6295_status_0_r -- generic status read functions
  785.      OKIM6295_status_1_r
  786.  
  787. ***********************************************************************************************/
  788.  
  789. READ_HANDLER( OKIM6295_status_0_r )
  790. {
  791.     return OKIM6295_status_r(0);
  792. }
  793.  
  794. READ_HANDLER( OKIM6295_status_1_r )
  795. {
  796.     return OKIM6295_status_r(1);
  797. }
  798.  
  799.  
  800.  
  801. /**********************************************************************************************
  802.  
  803.      OKIM6295_data_0_w -- generic data write functions
  804.      OKIM6295_data_1_w
  805.  
  806. ***********************************************************************************************/
  807.  
  808. WRITE_HANDLER( OKIM6295_data_0_w )
  809. {
  810.     OKIM6295_data_w(0, data);
  811. }
  812.  
  813. WRITE_HANDLER( OKIM6295_data_1_w )
  814. {
  815.     OKIM6295_data_w(1, data);
  816. }
  817.